#!/usr/bin/env python
# vi: et sw=2
#
# AppletGConf.py
# Copyright (C) 2009  Harry Karvonen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Author: Harry Karvonen <harry.karvonen@gmail.com>
#         Aki Rautio <aki.rautio@gmail.com>
#

import gconf
import pygtk
import gtk

from PulseAppletGlobals import *

## FIXME: Add Description to Gconf items. See gconf-editor...

################################################################################
#
# Config
class AppletGConfig:

  def __init__(self):
    self.gconf            = gconf.client_get_default()
    self.base             = CONF_DIR + "/"    

    self.default_values   = self._get_default_valuelist()
    
    if not self.gconf.dir_exists(CONF_DIR):
      self.gconf.add_dir(CONF_DIR,gconf.CLIENT_PRELOAD_NONE)

    #
    # __init__ end
    #

  def _get_default_valuelist(self):
    return {
      "layout":         "vertical",
      "sink":           0,
      "icon_use_xml":   True,
      "show_vol_amount":True,
      "theme":          THEME_DEFAULT
    }

    #
    # _get_default_valuelist end
    #

  def connect(self, name, function):
    if type(name).__name__ == "list":
      for item in name:
        self.gconf.notify_add(self.base + item, function)    
        return True    
    elif type(name).__name__ == "str":
      self.gconf.notify_add(self.base + name, function)
      return True

    return False

    #
    # connect end
    #
  
  def get_value(self, name):
    if self.gconf.get(self.base + name) != None:
      if self.gconf.get(self.base + name).type == gconf.VALUE_STRING:
        return self.gconf.get_string(self.base + name)
      elif self.gconf.get(self.base + name).type == gconf.VALUE_BOOL:
        return self.gconf.get_bool(self.base + name)
    else:
      return self._create_default_value(name)

    return None

    #
    # get_value end
    #

  def get_layout(self):
    filename = self.get_value('theme')    
    layout = gtk.Builder()
    try:
      layout.add_from_file(filename)
    except:
      # try default value
      # FIXME notify user about that except
      layout.add_from_file(THEME_DEFAULT)
      self.set_value("theme", THEME_DEFAULT)

    return layout

    #
    # get_layout end
    #

  def get_view_settings(self):
    if self.get_value('layout') == "vertical":
      box = gtk.HBox
    else:
      box = gtk.VBox

    return {
      "layout_function" : self.get_layout,
      "icon_user_xml"   : self.get_value('icon_use_xml'),
      "box_function"    : box,
      "show_vol_amount" : self.get_value('show_vol_amount')
    }

    #
    # get_view_settings end
    #

  def set_value(self, name, value):
    if name in self.default_values:
      if type(value).__name__ == 'str':
        self.gconf.set_string(self.base + name, str(value))
        return True
      elif type(value).__name__ == 'bool':
        self.gconf.set_bool(self.base + name, bool(value))
        return True

    return False
   
    #
    # set_value end
    #

  def _create_default_value(self, name):
    if name in self.default_values and \
       self.set_value(name, self.default_values[name]):
      return self.get_value(name)    
    return None

    #
    # _create_default_value end
    #

  pass

  #
  # AppletGConf end
  #

